package cn.wsn2018.tools.service;

import cn.wsn2018.tools.common.CommonValue;
import cn.wsn2018.tools.dao.LogsInfoDao;
import cn.wsn2018.tools.dao.UserDao;
import cn.wsn2018.tools.model.Log.ErrorLogInfo;
import cn.wsn2018.tools.model.Log.LogsInfo;
import cn.wsn2018.tools.model.Log.WarningLogInfo;
import cn.wsn2018.tools.model.SystemConfig;
import cn.wsn2018.tools.model.User;
import cn.wsn2018.tools.util.*;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.util.JSONUtils;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.bson.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.*;

/**
 * @author gmz.
 * @date 2018/12/14.
 * @time 20:01
 */
@Service
public class LogsInfoService {

    @Autowired
    private LogsInfoDao logsInfoDao;

    @Autowired
    private UserDao userDao;
    private static final Logger logger = LoggerFactory.getLogger(LogsInfoService.class);

    /**
     * 根据用户名查询才用户所有日志信息
     * @param username
     * @return
     */
/*    public String findAllLogsByName(String username){
        if(username == null){
            return JsonUtil.failedJson(CommonValue.JSON_INPUT_ERROR);
        }
        List<LogsInfo> list = logsInfoDao.listLogsInfoByUser(username);
        String json = JSONArray.fromObject(list).toString();
        return json;
    }*/

    /**
     * 查询所有操作员、第X页的日志信息
     * @param pageNum
     * @param pageSize
     * @return
     */
    public String findLogsByPageNum(String pageNum,String pageSize){
        if((pageNum == null || pageNum.isEmpty())||(pageSize == null || pageSize.isEmpty())){
            return JsonUtil.failedJson(CommonValue.JSON_INPUT_ERROR);
        }
        int pageNumber = Integer.parseInt(pageNum);
        int size = Integer.parseInt(pageSize);
        //查询指定分页的内容
        List<LogsInfo> list = logsInfoDao.listLogsByPageNum(pageNumber,size);
        JSONArray json = JSONArray.fromObject(list);
        if(json.isEmpty() || json == null){
            return JsonUtil.failedJson(CommonValue.JSON_QUERY_FAILED);
        }else {
            System.out.println(json);
            return JsonUtil.successJson(CommonValue.JSON_QUERY_SUCCESS, null, json);
        }
    }

    /**
     * 查询某一操作员、第X页的日志信息
     * @param username
     * @param pageSize
     * @param pageNum
     * @return
     */
    public String findLogsByPgNumAndName(String username,String pageNum,String pageSize){
        if((username == null || username.isEmpty()) || (pageNum == null || pageNum.isEmpty())||(pageSize == null || pageSize.isEmpty())){
            return JsonUtil.failedJson(CommonValue.JSON_INPUT_ERROR);
        }
        int pageNumber = Integer.parseInt(pageNum);
        int size = Integer.parseInt(pageSize);
        //查询指定分页的内容
        List<LogsInfo> list = logsInfoDao.listLogsInfoByUser(username,pageNumber,size);
        JSONArray json = JSONArray.fromObject(list);
        if(json.isEmpty() || json == null){
            return JsonUtil.failedJson(CommonValue.JSON_QUERY_FAILED);
        }else {
            System.out.println(json);
            return JsonUtil.successJson(CommonValue.JSON_QUERY_SUCCESS, null, json);
        }
    }

    /**
     * 根据时间范围、操作员查询某一页日志信息
     * @param username
     * @param pageNum
     * @param pageSize
     * @param fromTime
     * @param toTime
     * @return
     */
    public String findLogsByTimeAndName(String username,String pageNum,String pageSize,String fromTime,String toTime){
        if((username == null || username.isEmpty())||(fromTime == null || fromTime.isEmpty()) ||(toTime == null ||
                toTime.isEmpty()) ||(pageNum == null || pageNum.isEmpty()) || (pageSize == null || pageSize.isEmpty())){
            return JsonUtil.failedJson(CommonValue.JSON_INPUT_ERROR);
        }
        int pageNumber = Integer.parseInt(pageNum);
        int size = Integer.parseInt(pageSize);
        //查询指定分页的内容
        List<LogsInfo> list = logsInfoDao.listLogsInfoByTimeAndName(username,pageNumber,size,fromTime,toTime);
        JSONArray json = JSONArray.fromObject(list);
        if(json.isEmpty()){
            return JsonUtil.failedJson(CommonValue.JSON_QUERY_FAILED);
        }else {
            System.out.println(json);
            return JsonUtil.successJson(CommonValue.JSON_QUERY_SUCCESS, null, json);
        }
    }

    /**
     * 查询时间范围内、所有操作员、某一页的日志信息
     * @param pageNum
     * @param pageSize
     * @param fromTime
     * @param toTime
     * @return
     */
    public String findLogsByTime(String pageNum, String pageSize,String fromTime,String toTime){
        if((fromTime == null || fromTime.isEmpty()) ||(toTime == null || toTime.isEmpty()) ||
                (pageNum == null || pageNum.isEmpty()) || (pageSize ==null || pageSize.isEmpty())){
            return JsonUtil.failedJson(CommonValue.JSON_INPUT_ERROR);
        }
        int pageNumber = Integer.parseInt(pageNum);
        int size = Integer.parseInt(pageSize);
        //查询指定分页的内容
        List<LogsInfo> list = logsInfoDao.listLogsInfoByTime(pageNumber,size,fromTime,toTime);
        JSONArray json = JSONArray.fromObject(list);
            if(json.isEmpty()){
                return JsonUtil.failedJson(CommonValue.JSON_QUERY_FAILED);
        }else {
            System.out.println(json);
            return JsonUtil.successJson(CommonValue.JSON_QUERY_SUCCESS, null, json);
        }
    }

    /**
     * @Description: 条件查询业务日志
     * @Param: [username, eventType, pageNum, pageSize, fromTime, toTime, timeSort, usernameSort, typeSort, eventTypeSort, myUsername, click]
     * @return: java.lang.String
     * @Author: SongDaisong
     * @Date:  2020/2/19 21:23
     */
    public String findLogsByCond(String username,Integer eventType, String pageNum, String pageSize, String fromTime, String toTime,
                                 String timeSort, String usernameSort, String typeSort,String eventTypeSort, String myUsername,Boolean click){

        if((pageNum == null || pageNum.isEmpty()) || (pageSize ==null || pageSize.isEmpty())){
            return JsonUtil.failedJson(CommonValue.JSON_INPUT_ERROR);
        }

        //页号 页大小转换  时间校验
        int pageNumber = 1;
        int size = 10;

        try{
            pageNumber = Integer.parseInt(pageNum);
            size = Integer.parseInt(pageSize);
        }catch (Exception e){
            //页号、页大小转换失败，字符串非纯数字
            return JsonUtil.failedJson(CommonValue.JSON_INPUT_ERROR);
        }

        //时间校验
        if(!DateUtil.timeOrderCheck(fromTime,toTime)){
            //返回false
            return JsonUtil.failedJson(CommonValue.JSON_INPUT_ERROR);
        }

        //查询指定分页的内容
        List<LogsInfo> list = logsInfoDao.listLogsInfoByCond(username,eventType,pageNumber,size,fromTime,toTime,timeSort,usernameSort,typeSort,eventTypeSort);
        JSONArray resArray = JSONArray.fromObject(list);

        if(click){
            LogUtil.produceLogs(myUsername,CommonValue.SELECTED_TYPE,
                    "查询业务日志",CommonValue.LOG_TYPE_BUS,
                    CommonValue.SUCCESS);
        }

        if(resArray.isEmpty()){
            return JsonUtil.failedJson(CommonValue.JSON_QUERY_FAILED);
        }else {
            //统计符合条件的信息
            Long resCount = logsInfoDao.countLogsInfoByCond(username, fromTime, toTime,eventType);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("resCount",resCount);
            jsonObject.put("resArray",resArray);

            return JsonUtil.successJson(CommonValue.JSON_QUERY_SUCCESS, jsonObject, null);
        }
    }


    //查询错误日志/告警日志的分页
    public String listLogInfo(String username,String pageNum,String pageSize ,String fromTime,String toTime,Class className,String collectionName,
                              String moduleNameSort,String warnTypeSort,String warnLevelSort,String timeSort,
                              String errorModuleSort,String usernameSort,String operatTypeSort,Boolean click,String myUsername){

        //注意：usernameSort 由于后期告警日志/错误日志都可以对用户名进行排序，共用了，接口只在以前基础上改，比较难看

        if((pageNum == null || pageNum.isEmpty()) || (pageSize ==null || pageSize.isEmpty())){
            return JsonUtil.failedJson(CommonValue.JSON_INPUT_ERROR);
        }

        //时间格式转换，以及先后顺序检查
        if(!DateUtil.timeOrderCheck(fromTime,toTime)){
            return JsonUtil.failedJson(CommonValue.JSON_INPUT_ERROR);
        }


        List<LogsInfo> logInfoList = logsInfoDao.listLogInfoByCond(
                username,
                Integer.parseInt(pageNum),
                Integer.parseInt(pageSize),
                fromTime,
                toTime,
                className,
                collectionName,
                moduleNameSort,
                warnTypeSort,
                warnLevelSort,
                timeSort,
                errorModuleSort,
                usernameSort,
                operatTypeSort
        );
        JSONArray json = JSONArray.fromObject(logInfoList);

        if(click){
            //是点击搜索，需要记录插叙日志
            String msg="查询告警日志";;
            if(CommonValue.ERROR_COLLLECTION.equals(collectionName)){
                msg="查询错误日志";
            }
            LogUtil.produceLogs(myUsername,CommonValue.SELECTED_TYPE,
                    msg,CommonValue.LOG_TYPE_BUS,
                    CommonValue.SUCCESS);
        }

        if(json.isEmpty()){
            return JsonUtil.failedJson(CommonValue.JSON_QUERY_NULL);
        }else {
            //统计符合条件
            Long resCount = logsInfoDao.coutLogInfoByCond(username, fromTime, toTime, collectionName);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("resCount",resCount);
            jsonObject.put("resArray",json);
            return JsonUtil.successJson(CommonValue.JSON_QUERY_SUCCESS, jsonObject, null);
        }

    }

    /**
     * 根据日志类型是业务日志、告警日志还是系统错误日志来实现日志转存
     * @param type
     * @return
     */
    public SXSSFWorkbook storeLogsInfo(String username,String type){
        List<LogsInfo> list=new ArrayList<LogsInfo>();
        //json数组
        JSONArray json=new JSONArray();
        String msg="";
        if(type.equals("normal")){
            list=logsInfoDao.listAllLogsByCond(LogsInfo.class,"logsInfo");
            msg="备份业务日志";
        }
        else if(type.equals("warn")){
            list=logsInfoDao.listAllLogsByCond(WarningLogInfo.class,"warningLogInfo");
            msg="备份告警日志";
        }
        else if(type.equals("error")){
            list=logsInfoDao.listAllLogsByCond(ErrorLogInfo.class,"errorLogInfo");
            msg="备份错误日志";
        }
        //有限查询
        if(list.size()>20000)
            list=list.subList(0,20000);
        json= JSONArray.fromObject(list);
        //logger.info("json:"+json);
        SXSSFWorkbook workbook =JsonUtil.JSONToExcel(json,type);
        if(list.size()!=0){
            //备份成功
            LogUtil.produceLogs(username,CommonValue.BACK_UP_LOGS,msg,CommonValue.LOG_TYPE_BUS,CommonValue.SUCCESS);
            return workbook;
        }
        else
            return null;
    }

    /**
     * @Description: 条件统计业务日志
     * @Param: [fromTime, toTime, name, eventType, token, request]
     * @return: java.lang.String
     * @Author: SongDaisong
     * @Date:  2020/2/19 21:23
     */
    public String statisticalBusLogs(String fromTime, String toTime, String name,Integer eventType,
                                     String token, HttpServletRequest request) throws Exception {

        String res = RoleUtil.checkRole(token, request, CommonValue.ROLE_AUDITOR,CommonValue.AUDITOR_MODULE);
        if(!StringUtil.isEmptyString(res)){
            //如果结果字符串不为空，则表示角色校验失败，直接返回json字符串
            return res;
        }

        //时间校验
        if(!DateUtil.timeOrderCheck(fromTime,toTime)){
            //返回false
            return JsonUtil.failedJson(CommonValue.JSON_INPUT_ERROR);
        }

        //用户名是否存在
        if(!StringUtil.isEmptyString(name)){
            //用户名不为空时
            User user = userDao.getUserByName(name);
            if(user==null){
                return JsonUtil.failedJson(CommonValue.JSON_STATISTICAL_NULL);
            }
        }

        List<Document> list = logsInfoDao.statisticalBusLogs(fromTime, toTime, name, eventType);
        JSONArray jsonArray = JSONArray.fromObject(list);
        String msg =null;
        if(list==null||list.size()==0||list.isEmpty()){
            LogUtil.produceLogs(ServletUtil.getUserNameByToken(token),CommonValue.STATISTICAL_TYPE,"统计业务日志",CommonValue.LOG_TYPE_BUS,CommonValue.FAILED);
            msg = JsonUtil.failedJson(CommonValue.JSON_STATISTICAL_NULL);
        }else {
            JSONObject object = new JSONObject();
            object.put("name",name);
            object.put("fromTime",fromTime);
            object.put("toTime",toTime);
            object.put("eventCode",String.valueOf(eventType));
            object.put("dataArray",jsonArray);
            msg=JsonUtil.successJson(CommonValue.JSON_STATISTICAL_SUCCESS, object, null);
            LogUtil.produceLogs(ServletUtil.getUserNameByToken(token),CommonValue.STATISTICAL_TYPE,"统计业务日志",CommonValue.LOG_TYPE_BUS,CommonValue.SUCCESS);
        }

        return msg;
    }

    /**
     * @Description: 告警日志统计
     * @Param: [fromTime, toTime, name, warnType, token, request]
     * @return: java.lang.String
     * @Author: SongDaisong
     * @Date:  2020/2/23 15:27
     */
    public String statisticalWarnLogs(String fromTime, String toTime, String name,Integer warnType,
                                      String token, HttpServletRequest request) throws Exception {

        //角色校验
        String res = RoleUtil.checkRole(token, request, CommonValue.ROLE_SYS_AUDITOR,CommonValue.SYS_AUDITOR_MODULE);
        if(!StringUtil.isEmptyString(res)){
            //如果结果字符串不为空，则表示角色校验失败，直接返回json字符串
            return res;
        }

        //时间校验
        if(!DateUtil.timeOrderCheck(fromTime,toTime)){
            //返回false
            return JsonUtil.failedJson(CommonValue.JSON_INPUT_ERROR);
        }

        //用户名是否存在
        if(!StringUtil.isEmptyString(name)){
            //用户名不为空时
            User user = userDao.getUserByName(name);
            if(user==null){
                return JsonUtil.failedJson(CommonValue.JSON_STATISTICAL_NULL);
            }
        }

        List<Document> warningLogInfo = logsInfoDao.statisticalWarnLogs(fromTime, toTime, name, warnType);
        if(warningLogInfo==null||warningLogInfo.size()==0||warningLogInfo.isEmpty()){
            LogUtil.produceLogs(ServletUtil.getUserNameByToken(token),CommonValue.STATISTICAL_TYPE,"统计告警日志",CommonValue.LOG_TYPE_BUS,CommonValue.FAILED);
            return JsonUtil.failedJson(CommonValue.JSON_STATISTICAL_NULL);
        }else{
            JSONArray jsonArray = JSONArray.fromObject(warningLogInfo);
            JSONObject resObj = new JSONObject();
            resObj.put("fromTime",fromTime);
            resObj.put("toTime",toTime);
            resObj.put("name",name);
            resObj.put("warnType",String.valueOf(warnType));
            resObj.put("dataArray",jsonArray);
            LogUtil.produceLogs(ServletUtil.getUserNameByToken(token),CommonValue.STATISTICAL_TYPE,"统计告警日志",CommonValue.LOG_TYPE_BUS,CommonValue.SUCCESS);
            return JsonUtil.successJson(CommonValue.JSON_STATISTICAL_SUCCESS,resObj,null);
        }
    }

    /**
     * @Description: 统计错误日志
     * @Param: [fromTime, toTime, name, moduleType, token, request]
     * @return: java.lang.String
     * @Author: SongDaisong
     * @Date:  2020/2/23 15:51
     */
    public String statisticalErrorLogs(String fromTime, String toTime, String name,Integer moduleType,
                                      String token, HttpServletRequest request) throws Exception {
        //角色校验
        String res = RoleUtil.checkRole(token, request, CommonValue.ROLE_SYS_AUDITOR,CommonValue.SYS_AUDITOR_MODULE);
        if(!StringUtil.isEmptyString(res)){
            //如果结果字符串不为空，则表示角色校验失败，直接返回json字符串
            return res;
        }

        //时间校验
        if(!DateUtil.timeOrderCheck(fromTime,toTime)){
            //返回false
            return JsonUtil.failedJson(CommonValue.JSON_INPUT_ERROR);
        }

        //用户名是否存在
        if(!StringUtil.isEmptyString(name)){
            //用户名不为空时
            User user = userDao.getUserByName(name);
            if(user==null){
                return JsonUtil.failedJson(CommonValue.JSON_STATISTICAL_NULL);
            }
        }

        List<Document> list = logsInfoDao.statisticalErrorLogs(fromTime, toTime, name, moduleType);
        if(list==null||list.size()==0||list.isEmpty()){
            LogUtil.produceLogs(ServletUtil.getUserNameByToken(token),CommonValue.STATISTICAL_TYPE,"统计错误日志",CommonValue.LOG_TYPE_BUS,CommonValue.FAILED);
            return JsonUtil.failedJson(CommonValue.JSON_STATISTICAL_NULL);
        }else{
            JSONArray dataArray = JSONArray.fromObject(list);
            //存放最后返回给前端的json对象
            JSONObject resObj = new JSONObject();

            //统计日期
            TreeSet<String> dateTreeSet = new TreeSet<>();
            //存放五个模块每天日志数的map
            HashMap<String, Integer> loginMap = new HashMap<>();
            HashMap<String, Integer> operatorMap = new HashMap<>();
            HashMap<String, Integer> auditorMap = new HashMap<>();
            HashMap<String, Integer> sysAuditorMap = new HashMap<>();
            HashMap<String, Integer> adminMap = new HashMap<>();

            for(int i=0;i<dataArray.size();i++){
                //5个map：时间-各模块日志记录数
                JSONObject obj = JSONObject.fromObject(dataArray.get(i));
                String date=(String)obj.opt("date");
                dateTreeSet.add(date);
                Integer totalNum=(Integer)obj.opt("totalNum");
                String moduleName=(String)obj.opt("moduleName");
                if(!StringUtil.isEmptyString(moduleName)){
                    if(CommonValue.LOGIN_MODULE.equals(moduleName)){
                        //登录
                        loginMap.put(date,totalNum);
                    }else if(CommonValue.OPERATOR_MODULE.equals(moduleName)){
                        //操作员
                        operatorMap.put(date,totalNum);
                    }else if(CommonValue.AUDITOR_MODULE.equals(moduleName)){
                        //业务审计员
                        auditorMap.put(date,totalNum);
                    }else if(CommonValue.SYS_AUDITOR_MODULE.equals(moduleName)){
                        //系统审计员
                        sysAuditorMap.put(date,totalNum);
                    }else if(CommonValue.ADMIN_MODULE.equals(moduleName)){
                        //管理员
                        adminMap.put(date,totalNum);
                    }
                }
            }

            ArrayList<Integer> logsNumList = new ArrayList<>();
            Iterator<String> dateIterator = dateTreeSet.iterator();
            while (dateIterator.hasNext()){
                String dateStr = dateIterator.next();
                int sum=0;
                if(loginMap.get(dateStr)!=null){
                    Integer num = loginMap.get(dateStr);
                    sum+=num;
                }
                if(operatorMap.get(dateStr)!=null){
                    Integer num = operatorMap.get(dateStr);
                    sum+=num;
                }
                if(auditorMap.get(dateStr)!=null){
                    Integer num = auditorMap.get(dateStr);
                    sum+=num;
                }
                if(sysAuditorMap.get(dateStr)!=null){
                    Integer num = sysAuditorMap.get(dateStr);
                    sum+=num;
                }
                if(adminMap.get(dateStr)!=null){
                    Integer num = adminMap.get(dateStr);
                    sum+=num;
                }
                logsNumList.add(sum);
            }

            resObj.put("fromTime",fromTime);
            resObj.put("toTime",toTime);
            resObj.put("name",name);
            resObj.put("moduleType",String.valueOf(moduleType));
            resObj.put("resNumArray",JSONArray.fromObject(logsNumList));
            resObj.put("resDateArray",JSONArray.fromObject(dateTreeSet.toArray()));

            resObj.put("loginMap",loginMap);
            resObj.put("operatorMap",operatorMap);
            resObj.put("auditorMap",auditorMap);
            resObj.put("sysAuditorMap",sysAuditorMap);
            resObj.put("adminMap",adminMap);
            LogUtil.produceLogs(ServletUtil.getUserNameByToken(token),CommonValue.STATISTICAL_TYPE,"统计错误日志",CommonValue.LOG_TYPE_BUS,CommonValue.SUCCESS);
            return  JsonUtil.successJson(CommonValue.JSON_STATISTICAL_SUCCESS,resObj,null);
        }

    }

    public String getUserLogArrivedMax(String token, HttpServletRequest request) throws Exception {
        /**
        * @Description: 查询业务日志文件(本地)是否即将达到上限,超过阈值则给出提示
        * @Param: []
        * @return: java.lang.String
        * @Author: SongDaisong
        * @Date:  2020/4/17 16:30
        */

        //角色校验
        String res = RoleUtil.checkRole(token, request, CommonValue.ROLE_AUDITOR,CommonValue.AUDITOR_MODULE);
        if(!StringUtil.isEmptyString(res)){
            //如果结果字符串不为空，则表示角色校验失败，直接返回json字符串
            return res;
        }

        JSONObject obj = new JSONObject();
        File file = new File(CommonValue.USER_LOG);
        if (!file.exists()){
            //文件不存在
            obj.put("isAlert",false);
        }else{
            SystemConfig systemConfig = SystemConfig.getInstance();
            long maxVolume = Integer.valueOf(systemConfig.getLogMaxVolume())*CommonValue.ONE_GB;
            //设置弹窗的阈值
            double threshold=0.95;
            if (file.length()>=(maxVolume*threshold)){
                obj.put("isAlert",true);
               // obj.put("message","业务日志文件大小已达到最大上限的"+(threshold*100)+"%");
                obj.put("message","业务日志存储空间即将达到最大存储上限");
            }else {
                obj.put("isAlert",false);
            }
        }
        return JsonUtil.successJson(CommonValue.JSON_QUERY_SUCCESS,obj,null);
    }

    public String getWarnLogArrivedMax(String token, HttpServletRequest request) throws Exception {
        /**
        * @Description:  查询告警日志文件(本地)是否即将达到上限,超过阈值则给出提示
        * @Param: [token, request]
        * @return: java.lang.String
        * @Author: SongDaisong
        * @Date:  2020/4/17 18:22
        */
        //角色校验
        String res = RoleUtil.checkRole(token, request, CommonValue.ROLE_SYS_AUDITOR,CommonValue.SYS_AUDITOR_MODULE);
        if(!StringUtil.isEmptyString(res)){
            //如果结果字符串不为空，则表示角色校验失败，直接返回json字符串
            return res;
        }

        JSONObject obj = new JSONObject();
        File file = new File(CommonValue.WARN_LOG);
        if (!file.exists()){
            //文件不存在
            obj.put("isAlert",false);
        }else{
            SystemConfig systemConfig = SystemConfig.getInstance();
            long maxVolume = Integer.valueOf(systemConfig.getLogMaxVolume())*CommonValue.ONE_GB;
            //设置弹窗的阈值
            double threshold=0.95;
            if (file.length()>=(maxVolume*threshold)){
                obj.put("isAlert",true);
                obj.put("message","告警日志存储空间即将达到最大存储上限");
            }else {
                obj.put("isAlert",false);
            }
        }
        return JsonUtil.successJson(CommonValue.JSON_QUERY_SUCCESS,obj,null);
    }

    public String getErrorLogArrivedMax(String token, HttpServletRequest request) throws Exception {
        /**
         * @Description:  查询告警日志文件(本地)是否即将达到上限,超过阈值则给出提示
         * @Param: [token, request]
         * @return: java.lang.String
         * @Author: SongDaisong
         * @Date:  2020/4/17 18:22
         */
        //角色校验
        String res = RoleUtil.checkRole(token, request, CommonValue.ROLE_SYS_AUDITOR,CommonValue.SYS_AUDITOR_MODULE);
        if(!StringUtil.isEmptyString(res)){
            //如果结果字符串不为空，则表示角色校验失败，直接返回json字符串
            return res;
        }

        JSONObject obj = new JSONObject();
        File file = new File(CommonValue.ERROR_LOG);
        if (!file.exists()){
            //文件不存在
            obj.put("isAlert",false);
        }else{
            SystemConfig systemConfig = SystemConfig.getInstance();
            long maxVolume = Integer.valueOf(systemConfig.getLogMaxVolume())*CommonValue.ONE_GB;
            //设置弹窗的阈值
            double threshold=0.95;
            if (file.length()>=(maxVolume*threshold)){
                obj.put("isAlert",true);
                obj.put("message","错误日志存储空间即将达到最大存储上限");
            }else {
                obj.put("isAlert",false);
            }
        }
        return JsonUtil.successJson(CommonValue.JSON_QUERY_SUCCESS,obj,null);
    }
}
